using System;
using System.Data;
using System.Collections;

/*
    CssDom Library
    Copyright (C) 2007-2011  Ferenc Veres

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

namespace WPS.Libraries.CssDom
{
	/// <summary>
	/// The CSSRuleList class provides the abstraction of an ordered collection of CSS rules.
	/// The items in the CSSRuleList are accessible via an integral index, starting from 0.
	/// </summary>
	public class CSSRuleList
	{
		/// <summary>
		/// Private array of the contained CSSRule objects.
		/// </summary>
		private ArrayList items;

		#region CSSRuleList interface pubic properties

		/// <summary>
		/// The number of CSSRules in the list. The range of valid child rule indices is 0 to length-1 inclusive.
		/// </summary>
		public int length
		{
			get
			{
				return this.items.Count;
			}
		}

		/// <summary>
		/// Indexer to access CSSRule objects contained in the list. 
		/// For CssDom standard access use the item() function.
		/// </summary>
		/// <param name="index">Zero based index to retrieve a rule.</param>
		/// <returns>The requested CSSRule from the current rule list.</returns>
		public CSSRule this[int index]
		{
			get
			{
				if(this.items.Count > index)
				{
					return (CSSStyleRule)this.items[index];
				}
				else
				{
					return null;
				}
			}
		}

		#endregion

		#region Initialization

		/// <summary>
		/// Creates a new empty CSS Rule List to store the CSSRule objects in a CSSStyleSheet object.
		/// </summary>
		public CSSRuleList()
		{
			items = new ArrayList();
		}

		#endregion

		#region CSSRuleList interface methods

		/// <summary>
		/// Used to retrieve a CSS rule by ordinal index. 
		/// </summary>
		/// <param name="index">Zero based index to retrieve a rule.</param>
		/// <returns>The style rule at the index position in the CSSRuleList, 
		/// or null if that is not a valid index.</returns>
		public CSSRule item(int index)
		{
			if(this.items.Count > index)
			{
				return (CSSStyleRule)this.items[index];
			}
			else
			{
				return null;
			}
		}

		#endregion

		#region Implementation related, nonstandard methods

		/// <summary>
		/// Used to insert a new rule into the style sheet. CSSStyleSheet class calls this, in general,
		/// the CSSStyleSheet's insertRule should be called, this is just the internal implementation.
		/// </summary>
		/// <param name="rule">The parsable text representing the rule. For rule sets this contains both the selector and the style declaration. For at-rules, this specifies both the at-identifier and the rule content.</param>
		/// <param name="index">The index within the style sheet's rule list of the rule before which to insert the specified rule. If the specified index is equal to the length of the style sheet's rule collection, the rule will be added to the end of the style sheet.</param>
		/// <returns>The index within the style sheet's rule collection of the newly inserted rule.</returns>
		public int insertRule(string rule, int index)
		{
			if(index == -1)
			{
				index = this.items.Count;
			}
			this.items.Insert(index, new CSSStyleRule(rule));
			return index;
		}

		/// <summary>
		/// Used to delete a rule from the style sheet.
		/// </summary>
		/// <param name="index">The index within the style sheet's rule list of the rule to remove.</param>
		public void deleteRule(int index)
		{
			this.items.RemoveAt(index);
		}

		#endregion
	}
}
